<template>
    <div>
      <MHeader>列表页</MHeader>
      <div class="content" ref="scroll" @scroll="loadMore">
        <ul>
          <router-link v-for="(book, index) in books" :key="index" :to="{name: 'detail', params: {bid: book.bookId}}" tag="li">
            <img v-lazy="book.bookCover" :alt="book.bookName">
            <div class="div-right">
              <h4>{{book.bookName}}</h4>
              <p>{{book.bookInfo}}</p>
              <b>￥{{book.bookPrice}}</b>
              <div class="btn-list">
                <button @click.stop="remove(book.bookId)">删除</button>
                <button @click.stop="addCollect(book)">放入购物车</button>
              </div>
            </div>
          </router-link>
        </ul>
      </div>
    </div>
</template>
<script>
import MHeader from '@/base/MHeader'
import {removeBook, pagination} from '@/api'
import * as Types from '../store/mutations-types'
export default {
  created () {
    this.getData()
  },
  mounted () {
    // 获取到要拖拽的元素
    let scroll = this.$refs.scroll
    // 获取拖拽元素的偏移高度
    let top = scroll.offsetTop
    let distance = 0
    let isMove = false
    scroll.addEventListener('touchstart', (e) => {
      // 滚动条在最顶端时，并且当前盒子在顶端的时候才继续执行
      if (scroll.scrollTop !== 0 || scroll.offsetTop !== top) {
        return
      }
      // 手指点击的开始
      let start = e.touches[0].pageY
      let move = (e) => {
        isMove = true
        let current = e.touches[0].pageY
        // 获取拖拽的距离
        distance = current - start
        if (distance > 0) {
          if (distance <= 50) {
            scroll.style.top = distance + top + 'px'
          } else {
            distance = 50
            scroll.style.top = 50 + top + 'px'
          }
        } else {
          // 如果不在考虑范围内，移除move和end事件
          scroll.removeEventListener('touchmove', move)
          scroll.removeEventListener('touchend', end)
        }
      }
      let end = (e) => {
        if (!isMove) {
          return
        }
        isMove = false
        clearInterval(this.intervalTimer)
        this.intervalTimer = setInterval(() => {
          if (distance <= 0) {
            clearInterval(this.intervalTimer)
            distance = 0
            scroll.style.top = top + 'px'
            scroll.removeEventListener('touchmove', move)
            scroll.removeEventListener('touchend', end)
            this.offset = 0
            this.hasMore = true
            this.books = {}
            this.getData()
            return
          }
          distance -= 1
          scroll.style.top = distance + top + 'PX'
        }, 1)
      }
      scroll.addEventListener('touchmove', move)
      scroll.addEventListener('touchend', end)
    }, false)
  },
  data () {
    return {books: [], offset: 0, hasMore: true, isLoading: false}
  },
  components: {
    MHeader
  },
  methods: {
    async getData () {
      if (this.hasMore && !this.isLoading) {
        this.isLoading = true
        let {hasMore, books} = await pagination(this.offset)
        this.books = [...this.books, ...books]
        this.offset = this.books.length
        this.hasMore = hasMore
        this.isLoading = false
      }
    },
    async remove (id) {
      await removeBook(id)
      this.books = this.books.filter(item => item.bookId !== id)
    },
    more () {
      this.getData()
    },
    loadMore () {
      // 获取卷曲的高度，当前可见高度和总高度
      // 触发scroll事件，可能触发多次，所以先进来时来一个定时器，下次触发时将上一次定时器清除掉，直到最后一次触发才去获取数据
      // 防抖
      this.timer = setTimeout(() => {
        let {scrollTop, clientHeight, scrollHeight} = this.$refs.scroll
        if (scrollTop + clientHeight + 20 > scrollHeight) {
          this.getData()
        }
      }, 100)
    },
    addCollect (book) {
      this.$store.commit(Types.INCREMENT, book)
    }
  }
}
</script>
<style scoped>
  .content ul{
    padding: 10px;
  }
  .content ul li{
    display: flex;
    padding-bottom: 1px solid #f1f1f1;
  }
  .content ul li img{
    width: 35%;
    height: 150px;
  }
  .content h3{
    font-size: 20px;
    line-height: 25px;
  }
  .content p{
    color: #2a2a2a;
    line-height: 25px;
  }
  .content b{
    color: red;
  }
  .content button{
    display: block;
    width: auto;
    height: 25px;
    background: orangered;
    color: #fff;
    border: none;
    border-radius: 15px;
    outline: none;
    padding: 2px 12px;
  }
  .more{
    margin: 10px;
    background: #2afedd;
    height: 30px;
    line-height: 30px;
    text-align: center;
    font-size: 20px;
  }
  .btn-list{
    display: -webkit-box;
    justify-content: space-around;
  }
  .btn-list button {
    margin-left: 5px;
  }
  .div-right{
    width: 65%;
    margin-left: 8px;
  }
</style>
